package com.translate.web.service.impl;

import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.ReceivablesPicMapper;
import com.translate.entity.ReceivablesPic;
import com.param.contract.IIdParam;
import com.translate.web.param.contract.IReceivablesPicAddParam;
import com.translate.web.service.IReceivablesPicService;
import com.basic.service.impl.BaseServiceImpl;
import com.translate.vo.contract.IReceivablesPicVO;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service
public class ReceivablesPicServiceImpl extends BaseServiceImpl<ReceivablesPic> implements IReceivablesPicService {
	
	private RedisTemplate<String, List<ReceivablesPic>> redisTemplate;

	
	@Autowired
	public void setRedisTemplate(RedisTemplate<String, List<ReceivablesPic>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	public ReceivablesPicServiceImpl(ReceivablesPicMapper mapper) {
		super(mapper);
	}

	protected Class<ReceivablesPic> getEntityClass() {
		return ReceivablesPic.class;
	}
	
	protected ReceivablesPic getEntityInstance() {
		return new ReceivablesPic();
	}
	
	protected int getId(ReceivablesPic data) {
		return data.getId();
	}
	

	@Override
	public int batchInsert(String[] list, int receivablesId) {
		if (null == list) {
			return 0;
		}
		
		List<ReceivablesPic> ts = new ArrayList<>(); 
		
		ReceivablesPic pic;
		
		for (String s : list) {
			
			pic = new ReceivablesPic();
			
			pic.setOrderId(receivablesId);
			
			pic.setPic(s);
			
			ts.add(pic);
		}
		
		return this.batchInsert(ts);
	}

	@Override
	public <VO extends IReceivablesPicVO> List<VO> getPics(int orderId, Class<VO> target) {
		List<VO> vos = new ArrayList<>();
		
		VO vo;
		
		List<ReceivablesPic> orderPics = this.getReceivablesPicsToCache(orderId);
		
		try {
			
			for (ReceivablesPic orderPic : orderPics) {
				
				vo = target.newInstance();
				
				vo.setOrderId(orderPic.getOrderId());
				
				vo.setId(orderPic.getId());
				
				vo.setPic(orderPic.getPic());
				
				vos.add(vo);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		return vos;
		
	}
	
	/**
	 * 获取订单图片备注
	 * @param orderId
	 * @return
	 */
	protected List<ReceivablesPic> getReceivablesPicsToCache(int orderId) {
		
		String key = RedisConstant.getOrderPicKey(orderId);
		
		ValueOperations<String, List<ReceivablesPic>> valueOperations = this.redisTemplate.opsForValue();
		
		List<ReceivablesPic> orderPics = valueOperations.get(key);
		
		if (null != orderPics) {
			return orderPics;
		}
		
		orderPics = this.getDataByIds(orderId);
		
		if (null == orderPics) {
			orderPics = new ArrayList<>(0);
			
			valueOperations.set(key, orderPics, 3, TimeUnit.SECONDS);
			
			return orderPics;
		}
		
		valueOperations.set(key, orderPics, 15, TimeUnit.SECONDS);

		return orderPics;
	}
	
	
	protected ReceivablesPic[] getUpdateData(int size) {
		
		return new ReceivablesPic[size];
	}

	/**
	 * @param entity
	 * @param obtainedFromTheMap
	 * @param assocId  关联id 由子类指定
	 * @param user 不需要则填 <------> 0
	 */
	protected <S> void toEntity(ReceivablesPic entity, S obtainedFromTheMap, int assocId, int userId) {
		
		
		IReceivablesPicAddParam customerPoolAddMemberParam = (IReceivablesPicAddParam) obtainedFromTheMap;

		entity.setOrderId(assocId);

		Integer id = null;
		
		if (obtainedFromTheMap instanceof IIdParam) {
			id = ((IIdParam) obtainedFromTheMap).getId();
			
			if (null != id) {
				entity.setId(id);
			}
		}

		entity.setPic(customerPoolAddMemberParam.getPic());
		
	}

	/**
	 * 比较更新主要条件
	 * 
	 * @return
	 */
	protected <S> boolean compareMasterCondition(S condition, ReceivablesPic data) {

		IReceivablesPicAddParam customerPoolAddMemberParam = (IReceivablesPicAddParam) condition;

		String pic = customerPoolAddMemberParam.getPic();

		return  data.getPic().equals(pic);
		
	}
	

}